﻿#region Copyright(c) 1998-2014, Arnaud Colin Microsoft Public License (Ms-PL)
/* Copyright(c) 1998-2014, Arnaud Colin
 * All rights reserved.
 */
#endregion
using System;
using System.Collections.Generic;
using System.Threading;


namespace T125Protocol
{
    public class CodeIds : List<CodeId>
    {
        public CodeId GetFree()
        {
            var g = Count;
            Random rGen = new Random();
            int num = rGen.Next(0, g);

            while ( !this[num].Mutex.WaitOne(0) )
            {
                num = rGen.Next(0, g);
            }
           
            return this[num];
        }
    }

    public class CodeId
    {
        private ushort iCode;
        public CodeId(ushort pid)
        {
            this.iCode = pid;
        }
      /* public string Code 
       {
           get
           {
               return string.Format("PID{0}", iCode);
           }
       }*/

       public override string ToString()
       {
           return string.Format("PID{0}", iCode);
       }

       public Mutex Mutex = new Mutex();
    }


}


